﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using AdmBase;
using Guedes_Library;

public partial class admin_pages_comentario_Novo : TelaBase
{

    private ComentarioBusiness business;

    public ComentarioBusiness Business
    {
        get {
            if (business == null)
                business = new ComentarioBusiness();
            return business; }
        set { business = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        IsLogado();
        string id = Request.QueryString["id"];

        if (id != null && !IsPostBack)
        {
            coments c = Business.GetById(int.Parse(id));
            if (c == null)
                Response.Redirect("~/admin/pages/comentario/Default.aspx");
            carregaAlterar(c);
        }
    }


    public void carregaAlterar(coments c)
    {
        txtDescricao.Text = c.Txt;
        txtNome.Text = c.Name;
        hdId.Value = c.ID.ToString();
        check.Checked = c.Permission;
        hdAvaliacao.Value = c.Rating.ToString();
        hdProduto.Value = c.ID_Prod.ToString();
        rating.ImageUrl = ResolveClientUrl("~/galeria/estrela" + c.Rating.ToString() + ".png");

    }

    protected void Salvar(object sender, EventArgs e)
    {
        coments c = new coments();

        if (hdId.Value != "")
            c.ID = int.Parse(hdId.Value);
        c.Name = txtNome.Text;
        c.Txt = txtDescricao.Text;
        c.Permission = check.Checked;
        c.Rating = int.Parse(hdAvaliacao.Value);
        c.ID_Prod = int.Parse(hdProduto.Value);

        Retorno retorno = Business.Salvar(c);
        if (retorno.Error)
            ShowMensagem("Atenção", retorno.Mensagem);
        else
        {
            Session["msg"] = retorno.Mensagem;
            Response.Redirect("~/admin/pages/comentario/Default.aspx");
        }

        
    }


    public void Limpar()
    {
        txtDescricao.Text = String.Empty;
        txtNome.Text = String.Empty;
        hdId.Value = String.Empty;

    }

}